package com.yss.shopping.user.config;

import com.yss.shopping.security.auth.PrivilegeUserDetails;
import com.yss.shopping.security.vo.PrivilegeUserVO;
import com.yss.shopping.user.service.user.SysUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p>
 * 初始化Security的userDetailsService
 * </p>
 *
 * @author yss
 * @since 2020-09-26 17:01
 */
@Component
public class InitUserDetailService {

    @Resource
    private SysUserService sysUserService;


    /**
     * 用于根据用户名获取用户信息和权限
     */
    @Bean
    public UserDetailsService userDetailsService() {
        return username -> {
            PrivilegeUserVO privilegeUserVO = this.sysUserService.selectPrivilegeUserVO(username);
            if (null != privilegeUserVO) {
                return new PrivilegeUserDetails(privilegeUserVO);
            }
            throw new UsernameNotFoundException("用户名或密码错误");
        };
    }


}
